package com.fadadianqi.logistics.inventory.service;

import com.fadadianqi.logistics.admin.controller.BacktrackController.BacktrackForm;
import com.fadadianqi.logistics.admin.controller.BacktrackController.BacktrackItemForm;
import com.fadadianqi.logistics.admin.printer.BacktrackPrinter;
import com.fadadianqi.commons.PrimaryKeyCreator;
import com.fadadianqi.logistics.inventory.dao.BacktrackBatchDAO;
import com.fadadianqi.logistics.inventory.dao.BacktrackDAO;
import com.fadadianqi.logistics.inventory.dao.BacktrackItemDAO;
import com.fadadianqi.logistics.inventory.model.*;
import com.yeqiangwei.commons.util.Pagination;
import com.yeqiangwei.commons.util.ValidatorHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class BacktrackService {

    public List<Backtrack> findAll(Pagination pagination) {
        return backtrackDAO.findAll(pagination);
    }

    @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = Throwable.class)
    public void doBack(BacktrackForm form) throws Exception {
        if (ValidatorHelper.isEmpty(form)) {
            return;
        }
        Backtrack b = new Backtrack();
        b.setAddTime(new Date());
        b.setId(PrimaryKeyCreator.create());
        b.setIsDeleted(Boolean.FALSE);
        b.setMemo(form.getMemo());
        b.setMoney(form.getMoney());
        b.setStaffId(null);
        backtrackDAO.create(b);
        //
        for (BacktrackItemForm item : form.getItems()) {
            Batch batch = inventoryService.getBatchById(item.getBatchId());
            //
            BacktrackBatch bb = new BacktrackBatch();
            bb.setBacktrackId(b.getId());
            bb.setBatchId(batch.getId());
            bb.setCount(item.getCount());
            bb.setId(PrimaryKeyCreator.create());
            bb.setGoodsId(batch.getGoodsId());
            bb.setBrandId(batch.getBrandId());
            bb.setCategoryId(batch.getCategoryId());
            backtrackBatchDAO.create(bb);

            for (int i = 0; i < item.getCount(); i++) {
                Item m = inventoryService.getNormalItemByGoodsId(batch.getGoodsId());
                if (item != null) {
                    m.setStatus(Item.STATUS_RETURN);
                    inventoryService.update(m);
                }
                BacktrackItem bi = new BacktrackItem();
                bi.setBacktrackId(b.getId());
                bi.setBacktrackBatchId(bb.getId());
                bi.setBatchId(batch.getId());
                bi.setId(PrimaryKeyCreator.create());
                bi.setItemId(m.getId());
                backtrackItemDAO.create(bi);
            }
        }
        if (form.getNeedPrint()) {
            backtrackPrinter.doPrint(b.getId());
        }
    }

    @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = Throwable.class)
    public void cancel(String id) {
        Backtrack b = backtrackDAO.getById(id);
        b.setIsDeleted(Boolean.TRUE);
        backtrackDAO.update(b);
        List<BacktrackBatch> bbList = backtrackBatchDAO.findBatchByBacktrackId(id);
        for (BacktrackBatch bb : bbList) {
            List<BacktrackItem> biList = backtrackItemDAO.findByBacktrackBatchId(bb.getId());
            for (BacktrackItem bi : biList) {
                Item m = inventoryService.getById(bi.getItemId());
                m.setStatus(Item.STATUS_NORMAL);
                inventoryService.update(m);
            }
        }
    }

    public List<BacktrackBatch> findBatchByBacktrackId(String backtrackId) {
        return backtrackBatchDAO.findBatchByBacktrackId(backtrackId);
    }

    public Backtrack getBacktrackById(String backtrackId) {
        return backtrackDAO.getById(backtrackId);
    }

    public void doPrint(String id) throws Exception {
        backtrackPrinter.doPrint(id);
    }

    public int count() {
        return backtrackDAO.count();
    }

    @Autowired
    private BacktrackPrinter backtrackPrinter;

    @Autowired
    private BacktrackItemDAO backtrackItemDAO;

    @Autowired
    private BacktrackBatchDAO backtrackBatchDAO;

    @Autowired
    private BacktrackDAO backtrackDAO;

    @Autowired
    private InventoryService inventoryService;
}
